package org.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.system.mapper.BizCarouselMapper;
import org.example.system.model.entity.BizCarousel;
import org.example.system.model.vo.CarouselVO;
import org.example.system.service.BizCarouselService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 小程序首页轮播图业务接口实现类
 * @author khb78
 */
@Service
@RequiredArgsConstructor
public class BizCarouselServiceImpl extends ServiceImpl<BizCarouselMapper, BizCarousel> implements BizCarouselService {

    private final BizCarouselMapper bizCarouselMapper;

    /**
     * 获取小程序首页轮播图列表
     * @return 小程序首页轮播图列表
     */
    @Override
    public List<CarouselVO> listBizCarousel() {
        // 从数据库中查询轮播图列表
        List<BizCarousel> bizCarouselList = bizCarouselMapper.selectList(
                new QueryWrapper<BizCarousel>()
        );

        // 转换为视图对象列表并返回
        List<CarouselVO> carouselVOList = bizCarouselList.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
        return carouselVOList;
    }

    // 将BizCarousel实体转换为BizCarouselVO视图对象的方法
    private CarouselVO convertToVO(BizCarousel bizCarousel) {
        CarouselVO vo = new CarouselVO();
        vo.setId(bizCarousel.getId());
        vo.setImgUrl(bizCarousel.getImageUrl());
        vo.setLinkUrl(bizCarousel.getLinkUrl());
        vo.setCreateTime(bizCarousel.getCreateTime());
        vo.setUpdateTime(bizCarousel.getUpdateTime());
        return vo;
    }
}
